//
//  NativeStoreCell.m
//  ebook
//
//  Created by mt on 17/1/9.
//  Copyright © 2017年 Moton. All rights reserved.
//

#define kBookNameFont 15.0  //书名字号
#define kTypeFont 13   //分类字号
#define kIntroductFont 12   //简介字号
#define kIntrodTextColor MTColor(149, 149, 149, 1.0)

#import "NativeStoreCell.h"
#import "Book.h"
#import "UIImageView+WebCache.h"

@interface NativeStoreCell ()

@property (nonatomic, strong) UIImageView *bookCoverImv;//封面
@property (nonatomic, strong) UILabel *bookNameLabel;   //书名
@property (nonatomic, strong) UILabel *bookStatusLabel; //状态
@property (nonatomic, strong) UILabel *bookTypeLabel;   //类型
@property (nonatomic, strong) UILabel *lineLabel;       // |
@property (nonatomic, strong) UILabel *bookAuthorLabel; //作者
@property (nonatomic, strong) UILabel *bookIntroLabel;  //简介

@end

@implementation NativeStoreCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self drawView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)drawView {
    
    [self addBookCoverImv];
    [self addSubview:self.bookNameLabel];
    [self addSubview:self.bookStatusLabel];
    [self addSubview:self.bookTypeLabel];
    [self addSubview:self.lineLabel];
    [self addSubview:self.bookAuthorLabel];
    [self addSubview:self.bookIntroLabel];
}

- (void)addBookCoverImv {
    
    UIImageView *backImv = [[UIImageView alloc] init];
    backImv.frame = CGRectMake(10, 10, (135.0-20)/1.3, 135.0-20);
    backImv.image = [UIImage imageNamed:@"bookcellBackground"];
    [self addSubview:backImv];
    
    UIImageView *coverImv = [[UIImageView alloc] init];
    coverImv.frame = CGRectMake(2, 2, CGRectGetWidth(backImv.frame)-4, CGRectGetHeight(backImv.frame)-4);
    [backImv addSubview:coverImv];
    _bookCoverImv = coverImv;
}

- (UILabel *)bookNameLabel {
    
    if (!_bookNameLabel) {
        _bookNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(12+CGRectGetWidth(_bookCoverImv.frame)+12, CGRectGetMinY(_bookCoverImv.frame)+2, 10, 30)];
        _bookNameLabel.font = [UIFont systemFontOfSize:kBookNameFont];
        _bookNameLabel.textColor = MTColor(59, 59, 59, 1.0);
    }
    return _bookNameLabel;
}

- (UILabel *)bookStatusLabel {
    
    if (!_bookStatusLabel) {
        _bookStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.bookNameLabel.frame)+8, CGRectGetMinY(_bookNameLabel.frame)+5, 40, 20)];
        _bookStatusLabel.textColor = [UIColor whiteColor];
        _bookStatusLabel.textAlignment = NSTextAlignmentCenter;
        _bookStatusLabel.backgroundColor = MTColor(245, 92, 80, 1);
        _bookStatusLabel.font = [UIFont systemFontOfSize:kTypeFont];
        _bookStatusLabel.layer.cornerRadius = 3;
        _bookStatusLabel.clipsToBounds = YES;
        _bookStatusLabel.hidden = kIsShowFullState!=1;
    }
    return _bookStatusLabel;
}

- (UILabel *)bookTypeLabel {
    
    if (!_bookTypeLabel) {
        _bookTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(12+CGRectGetWidth(_bookCoverImv.frame)+12, CGRectGetMaxY(self.bookNameLabel.frame), [self getWidthWithText:@"玄幻奇幻" fontSize:kTypeFont], 20)];
        _bookTypeLabel.font = [UIFont systemFontOfSize:kTypeFont];
        _bookTypeLabel.textColor = [UIColor orangeColor];
    }
    return _bookTypeLabel;
}

- (UILabel *)lineLabel {
    
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.bookTypeLabel.frame)+5, CGRectGetMaxY(self.bookNameLabel.frame)+2, 1, 16)];
        _lineLabel.backgroundColor = MTColor(190, 190, 190, 1);
    }
    return _lineLabel;
}

- (UILabel *)bookAuthorLabel {
    
    if (!_bookAuthorLabel) {
        _bookAuthorLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.lineLabel.frame)+5, CGRectGetMaxY(self.bookNameLabel.frame), 150, 20)];
        _bookAuthorLabel.font = [UIFont systemFontOfSize:kTypeFont];
        _bookAuthorLabel.textColor = kIntrodTextColor;
    }
    return _bookAuthorLabel;
}

- (UILabel *)bookIntroLabel {
    
    if (!_bookIntroLabel) {
        _bookIntroLabel = [[UILabel alloc] initWithFrame:CGRectMake(12+CGRectGetWidth(_bookCoverImv.frame)+12, CGRectGetMaxY(self.bookAuthorLabel.frame), MTScreenW-CGRectGetMaxX(_bookCoverImv.frame)-35, 135.0-CGRectGetMaxY(self.bookAuthorLabel.frame)-12)];
        _bookIntroLabel.numberOfLines=0;
        _bookIntroLabel.font = [UIFont systemFontOfSize:kIntroductFont];
        _bookIntroLabel.textColor = kIntrodTextColor;
        _bookIntroLabel.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
    }
    return _bookIntroLabel;
}

- (CGFloat)getWidthWithText:(NSString *)text fontSize:(CGFloat)size{
    return [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:size]}].width;
}

- (void)setModel:(NativeOrderlistModel *)model {
    
    [self.bookCoverImv sd_setImageWithURL:[NSURL URLWithString:model.coverpicurl]];
    self.bookNameLabel.width = [self getWidthWithText:model.bookname fontSize:kBookNameFont];
    self.bookNameLabel.text = model.bookname;
    self.bookAuthorLabel.text = model.author;
    self.bookTypeLabel.text = model.sortname;
    self.bookStatusLabel.x = CGRectGetMaxX(self.bookNameLabel.frame)+8;
    //0连载中   1已完结
    if ([model.fullflag intValue]==0) {
        _bookStatusLabel.backgroundColor = MTColor(245, 92, 80, 1);
        self.bookStatusLabel.text = @"连载";
    } else {
        _bookStatusLabel.backgroundColor = MTColor(84, 168, 144, 1);
        self.bookStatusLabel.text = @"完结";
    }
    
    //介绍文字需设置行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:model.introduction];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model.introduction length])];
    self.bookIntroLabel.attributedText = attributedString;
}

@end
